package com.xiong.word;

import com.xiong.word.utils.WordUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TestDemo {

      private static String PATH="C:\\Users\\86186\\Desktop\\新建 Microsoft Word 文档111.docx";

      @Test
      public void Test4() throws IOException {
            String path="D:\\dom\\花都区政务外网操作申请表（策略申请）20230222.docx";

            // 加载Word模板
            XWPFDocument document= WordUtils.getXWPFDocument(path);
            // 替换变量名为具体内容
            WordUtils.replaceVariable(document, "num", "1024");
            WordUtils.replaceVariable(document, "year", "2023");
            // 遍历文档中的段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                  // 获取段落文本
                  String paragraphText = paragraph.getText();
                  System.out.println("Paragraph: " + paragraphText);
            }
            // 获取第一个表格
            XWPFTable table = document.getTables().get(0);
            // 添加数据到表格，获取有多少行
            List<XWPFTableRow> rows = table.getRows();
            for (int i = 0; i < rows.size(); i++) { // 从第二行开始，忽略表头
                  XWPFTableRow row = rows.get(i);
                  //获取一行的每列数据
                  List<XWPFTableCell> cells = row.getTableCells();
                  for (int j = 0; j < cells.size(); j++) {
                        XWPFTableCell cell=cells.get(j);
                        System.out.println(i+"-" +j+". :"+cell.getText());
                        if(i==2 && j==1){
                              WordUtils.setValue(cell,"周大波");
                        }
                        if(i==18 && j==1){
                              WordUtils.replaceVariable(cell,"${date}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                        }
                  }
            }
            // 保存到新文件
            FileOutputStream output = new FileOutputStream("D:\\dom\\花都区政务外网操作申请表（策略申请）202302221.docx");
            document.write(output);
            output.close();
            document.close();
            System.out.println("Word文件生成成功！");
      }


      @Test
      public void Test3(){
            String path="D:\\dom\\花都区政务外网操作申请表（策略申请）20230222.docx";
            WordEngine wordEngine=new WordEngine(path);
            wordEngine.dealXWPFParagraph((e)->{
                  System.out.println(e.getText());
            });

            //处理第一个表格的数据
            wordEngine.dealTable(0).forEachRow((index,e)->{

                  List<XWPFTableCell> cells=e.getCells();
                  for (int i = 0; i < cells.size() ; i++) {
                        XWPFTableCell cell=cells.get(i);
                        System.out.println(index);
                       // System.out.println(index[0]+" 行 "+i+" : "+cell.getText());
//                        if(index==3 && i==1){
//                              cell.setText("周大波");
//                        }
                  }
            });
            wordEngine.outPath(path);
      }


      @Test
      public void Test2(){
           WordEngine wordEngine=new WordEngine(PATH);
           wordEngine.dealXWPFParagraph((e)->{
                 System.out.println(e.getText());
           });
           wordEngine.dealTable(0).forEachRow((index,e)->{
                 List<XWPFTableCell> cells=e.getCells();
                 for (int i = 0; i < cells.size() ; i++) {
                       System.out.println(cells.get(i).getText());
                 }
           });


      }


      @Test
      public void Test1() throws IOException {

            // 加载Word模板
            XWPFDocument document= WordUtils.getXWPFDocument(PATH);
            // 替换变量名为具体内容
            WordUtils.replaceVariable(document, "num", "1024");
            WordUtils.replaceVariable(document, "year", "2023");
            // 遍历文档中的段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                  // 获取段落文本
                  String paragraphText = paragraph.getText();
                  System.out.println("Paragraph: " + paragraphText);
            }


            // 获取第一个表格
            XWPFTable table = document.getTables().get(0);
            // 添加数据到表格，获取有多少行
            List<XWPFTableRow> rows = table.getRows();
            for (int i = 1; i < rows.size(); i++) { // 从第二行开始，忽略表头
                  XWPFTableRow row = rows.get(i);
                  //获取一行的每列数据
                  List<XWPFTableCell> cells = row.getTableCells();
                  cells.get(0).setText("Data " + i); // 在第一列添加数据
                  cells.get(1).setText("Data " + (i * 2)); // 在第二列添加数据
                  if(i==3){
                        //获取里面的表格数据
                        XWPFTable table2=cells.get(2).getTables().get(0);
                        int rowsnum=table2.getRows().size();
                        System.out.println(rowsnum);
                        List<XWPFTableRow> rows2 = table2.getRows();
                        for(XWPFTableRow e:rows2){
                              List<XWPFTableCell> cells2 =e.getTableCells();
                              cells2.get(0).setText("1111111");
                              cells2.get(1).setText("222222");
                        }
                        for (int j = 0; j < 20 ; j++) {
                              XWPFTableRow e=table2.createRow();
                              List<XWPFTableCell> cells2 =e.getTableCells();
                              cells2.get(0).setText(j+" 11qqqqq");
                              cells2.get(1).setText(j+" 22qqqqq");
                        }
                  }
            }
            // 保存到新文件
            FileOutputStream output = new FileOutputStream("C:\\Users\\86186\\Desktop\\新建 Microsoft Word 文档1113.docx");
            document.write(output);
            output.close();
            document.close();
            System.out.println("Word文件生成成功！");
      }
}
